<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="切割机名称" prop="devicename">
      <el-input v-model="dataForm.devicename" placeholder="切割机名称"></el-input>
    </el-form-item>
    <el-form-item label="切割进度状态" prop="deviceprocessstatus">
      <el-input v-model="dataForm.deviceprocessstatus" placeholder="切割进度状态"></el-input>
    </el-form-item>
    <el-form-item label="切割速度" prop="devicemachinespeed">
      <el-input v-model="dataForm.devicemachinespeed" placeholder="切割速度"></el-input>
    </el-form-item>
    <el-form-item label="切割系统状态" prop="devicecutstatus">
      <el-input v-model="dataForm.devicecutstatus" placeholder="切割系统状态"></el-input>
    </el-form-item>
    <el-form-item label="设备位置" prop="devicelocation">
      <el-input v-model="dataForm.devicelocation" placeholder="设备位置"></el-input>
    </el-form-item>
    <el-form-item label="报警状态" prop="devicestatus">
      <el-input v-model="dataForm.devicestatus" placeholder="报警状态"></el-input>
    </el-form-item>
    <el-form-item label="时间" prop="timestamp">
      <el-input v-model="dataForm.timestamp" placeholder="时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          devicename: '',
          deviceprocessstatus: '',
          devicemachinespeed: '',
          devicecutstatus: '',
          devicelocation: '',
          devicestatus: '',
          timestamp: ''
        },
        dataRule: {
          devicename: [
            { required: true, message: '切割机名称不能为空', trigger: 'blur' }
          ],
          deviceprocessstatus: [
            { required: true, message: '切割进度状态不能为空', trigger: 'blur' }
          ],
          devicemachinespeed: [
            { required: true, message: '切割速度不能为空', trigger: 'blur' }
          ],
          devicecutstatus: [
            { required: true, message: '切割系统状态不能为空', trigger: 'blur' }
          ],
          devicelocation: [
            { required: true, message: '设备位置不能为空', trigger: 'blur' }
          ],
          devicestatus: [
            { required: true, message: '报警状态不能为空', trigger: 'blur' }
          ],
          timestamp: [
            { required: true, message: '时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cuthisdata/cuthis/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.devicename = data.cuthis.devicename
                this.dataForm.deviceprocessstatus = data.cuthis.deviceprocessstatus
                this.dataForm.devicemachinespeed = data.cuthis.devicemachinespeed
                this.dataForm.devicecutstatus = data.cuthis.devicecutstatus
                this.dataForm.devicelocation = data.cuthis.devicelocation
                this.dataForm.devicestatus = data.cuthis.devicestatus
                this.dataForm.timestamp = data.cuthis.timestamp
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cuthisdata/cuthis/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'devicename': this.dataForm.devicename,
                'deviceprocessstatus': this.dataForm.deviceprocessstatus,
                'devicemachinespeed': this.dataForm.devicemachinespeed,
                'devicecutstatus': this.dataForm.devicecutstatus,
                'devicelocation': this.dataForm.devicelocation,
                'devicestatus': this.dataForm.devicestatus,
                'timestamp': this.dataForm.timestamp
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
